const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

//获取应用实例
var app = getApp()
Page({
  data: {
    inputViewShow: false,
    userInfo: {},
    userData: {},
    showLoading: false,
  },
  onLoad: function() {
    var that = this
    var user = wx.getStorageSync("user");
    if (user) {
      that.setData({
        userInfo: user
      });
    }
  },
  onShow: function() {
    // 生命周期函数--监听页面显示
    //获取用户信息
    this.getUserInfo();
  },
  //获取用户信息
  getUserInfo: function() {
    var that = this;
    http.ApiGet(apiUrl.UrlUserInfo, {}, function(res) {
      console.log(res);
      if (res.code == 1) {
        var user = res.data.userinfo;
        that.setData({
          userInfo: user,
        });
        wx.setStorageSync("user", user);
      }
    });
  },
  doLogout: function() {
    wx.showModal({
      title: '确定退出？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          // wx.clearStorage();
          wx.removeStorageSync("keeplogin");
          wx.removeStorageSync("tabBar");
          wx.removeStorage({
            key: 'user',
            complete: function(res) {
              // wx.setStorageSync("clearStorageTemp", true);
            }
          })
          wx.reLaunch({
            url: '../login/index'
          })
        }
      }
    })
  },
  doEditPost: function(postData) {
    var that = this;

    http.ApiPost(apiUrl.UrlInfoEdit, postData, function(res) {
      if (res.code == 1) {
        wx.hideToast()
        wx.showToast({
          title: '修改成功！',
          icon: 'success',
          duration: 3000
        });
        that.getUserInfo();
        setTimeout(function() {
          wx.hideToast();
        }, 1000)
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  closeInput: function() {
    this.setData({
      inputViewShow: false,
    });
  },
  nickNameInput: function(e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  nickNameTap: function() {
    // this.setData({
    //   inputViewShow: true,
    // });
  },
  doOK: function() {
    var that = this;
    var nickName = this.data.nickName;
    if (nickName == "") {
      wx.showModal({
        title: '昵称不能为空'
      });
      return;
    }
    that.closeInput();
    that.doEditPost({
      'nickname': nickName
    });
  },
  chooseImage: function() {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        for (let j = 0; j < res.tempFilePaths.length; ++j) {
          that.uploadImage(res.tempFilePaths[j])
        }
      }
    })
  },
  uploadImage: function(path) {
    var that = this
    that.showLoading();
    var postData = {};

    var user = that.data.userInfo;
    postData['appid'] = user.appid;
    postData['min_app'] = 1;
    wx.uploadFile({
      url: apiUrl.UrlUploadImg,
      filePath: path,
      name: 'file',
      formData: postData,
      success: function(res) {
        that.cancelLoading();
        var result = JSON.parse(res.data)
        // console.log(result);
        if (result.code == 1) {
          wx.hideToast()
          that.doEditPost({
            'avatar': result.data.url
          });
        } else {
          wx.showModal({
            title: result.msg
          });
        }
      },
      fail: function(err) {
        console.log(err);
        that.cancelLoading();
        wx.showModal({
          title: '上传出错！'
        });
      }
    })
  },
  showLoading: function() {
    this.setData({
      showLoading: true
    })
  },
  cancelLoading: function() {
    this.setData({
      showLoading: false
    })
  },
  changePwd: function() {
    console.log('跳转修改密码页面');
    wx.navigateTo({
      url: '../me/repwd'
    })
  },
})